package com.lanou.tipsy.fragment.mv;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.lanou.tipsy.R;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.entity.mv.mvInfo.MvInfo;
import com.lanou.tipsy.tools.volley.VolleySingleton;


/**
 * Created by wangshuai on 15/11/8.
 * MV播放界面fragment
 */
public class MvVideoInfoFragment extends BaseFragment {
    public static final String TAG = "MvVideoInfoFragment";
    private VideoView videoView;
    private TextView tv_video_mv;
    private Uri uri;
    private Context context;
    private String m;//
    private VolleySingleton volleySingleton;//单例
    private String mv_url;//数据接收实体类
    //返回按钮
    private ImageView mvBackIv;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_mv_video, null);
        videoView = (VideoView) view.findViewById(R.id.video_mv);
        tv_video_mv = (TextView) view.findViewById(R.id.tv_video_mv);
        mvBackIv = (ImageView) view.findViewById(R.id.mvBackIv);
        mvBackIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().getSupportFragmentManager().popBackStack();
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        init(getView());
    }

    // mv界面mv播放方法
    private void init(View view) {

        Bundle bundle = getArguments();
        m = bundle.getString("mv_id");
        String getUrl = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.2&method=baidu.ting.mv.playMV&format=json&mv_id=" +
                m + "&song_id=&definition=0";
        volleySingleton = VolleySingleton.getInstance();//单例添加队列
        StringRequest sr = new StringRequest(getUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("test" ,TAG +  response + ">>>>>>");
                //这里只能用JSON.parseObject 解析
                MvInfo mvInfo = JSON.parseObject(response, MvInfo.class);
                mv_url = mvInfo.getResult().getVideo_info().getSourcepath();
                tv_video_mv.setText(mvInfo.getResult().getMv_info().getTitle());
                uri = Uri.parse(mv_url);
                //设置视频
                videoView.setMediaController(new MediaController(context));
                videoView.setVideoURI(uri);
                videoView.start();
                videoView.requestFocus();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        //添加队列
        volleySingleton.addQueue(sr);

    }


}
